package spring.boot.zk.controller;


import io.swagger.annotations.ApiOperation;
import org.apache.zookeeper.CreateMode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.boot.zk.service.ZookeeperService;

import javax.annotation.Resource;
import java.util.List;


/**
 * 演示 Apache Curator API
 * 1、增删查改
 * 2、ACL 访问权限控制
 * 3、注册 watch 事件的三个接口
 */
@RestController
public class ZkTestController {


    @Resource
    private ZookeeperService zookeeperService ;

    @ApiOperation(value="创建并设置节点数据")
    @GetMapping("/createNodeAndData")
    public String createNodeAndData (Integer mode, String path , String nodeData){
        zookeeperService.createNodeAndData(mode,path,nodeData) ;
        return "success" ;
    }

    @ApiOperation(value="创建节点")
    @GetMapping("/createNode")
    public String createNode (CreateMode mode, String path ){
        zookeeperService.createNode(mode,path) ;
        return "success" ;
    }

    @ApiOperation(value="设置节点数据")
    @GetMapping("/setNodeData")
    public String setNodeData (String path, String nodeData) {
        zookeeperService.setNodeData(path,nodeData) ;
        return "success" ;
    }

    @ApiOperation(value="查询节点数据")
    @GetMapping("/getNodeData")
    public String getNodeData (String path) {
        return zookeeperService.getNodeData(path) ;
    }

    @ApiOperation(value="判断节点是否存在")
    @GetMapping("/isExistNode")
    public boolean isExistNode (final String path){
        return zookeeperService.isExistNode(path) ;
    }


    @ApiOperation(value="递归获取节点数据")
    @GetMapping("/getNodeChild")
    public List<String> getNodeChild (String path) {
        return zookeeperService.getNodeChild(path) ;
    }


    @ApiOperation(value="是否递归删除节点")
    @GetMapping("/deleteNode")
    public String deleteNode (String path,Boolean recursive) {
        zookeeperService.deleteNode(path,recursive) ;
        return "success" ;
    }

    @Deprecated
    @ApiOperation(value="watch监听")
    @GetMapping("/watchPath")
    public String watchPath (String path) {
        zookeeperService.watchPath(path) ;
        return "success" ;
    }

    @ApiOperation(value="cache监听")
    @GetMapping("/pathChildCachePath")
    public String pathChildCachePath (String path) {
        zookeeperService.pathChildCachePath(path) ;
        return "success" ;
    }

    @ApiOperation(value="cache监听")
    @GetMapping("/nodeCachePath")
    public String nodeCachePath (String path) {
        zookeeperService.nodeCachePath(path) ;
        return "success" ;
    }


    @ApiOperation(value="cache监听")
    @GetMapping("/treeCachePath")
    public String treeCachePath (String path) {
        zookeeperService.treeCachePath(path) ;
        return "success" ;
    }

    /**
     * 计数器
     * @param path
     * @return
     * @throws Exception
     */
    @ApiOperation(value="创建计数器")
    @GetMapping("/counter")
    public Long counter(String path) throws Exception {
        return zookeeperService.counter(path);
    }
    @ApiOperation(value="获取技术器值")
    @GetMapping("/getCounter")
    public Long getCounter(String path) throws Exception {
        return zookeeperService.getCounter(path);
    }


}